#ifndef	__ZEALINK_SAFE_FILE__
#define	__ZEALINK_SAFE_FILE__
/////////////////////////////////////////////////////////////////////////////
class NOVTABLE safe_file
{
public:
	BOOL	Open(LPCTSTR filename,BOOL blNew,BOOL blShared);
	void	Close(void);
	BOOL	IsOpend(void);

	BOOL	ReadFromFile(LPVOID buffer,DWORD offset,DWORD size,BOOL blLock);
	BOOL	WriteToFile(LPCVOID buffer,DWORD offset,DWORD size,BOOL blLock);
	BOOL	Append(DWORD newPages);

	inline	BOOL FileSeek(DWORD offset)
	{
#ifdef	WIN32
		if(curr_seek!=offset)
		{
			if(SetFilePointer(hFile,offset,NULL,FILE_BEGIN)==0xFFFFFFFF)
			{
				curr_seek = -1;
				return(FALSE);
			}
			curr_seek = offset;
		}
#else
		if(curr_seek!=offset)
		{
			if(fseek(hFile,offset,SEEK_SET))
			{
				curr_seek = -1;
				return(FALSE);
			}
			curr_seek = offset;
		}
#endif
		return(TRUE);
	}

	safe_file(void);
	~safe_file(void);

	safe_file	*pnext;
	read_cache	rdcache;
#ifdef	WIN32
	HANDLE	hFile;
#else
	FILE	*hFile;
#endif
	CRITICAL_SECTION csFile,csWrite;
	DWORD	num_page,curr_seek;
};
/////////////////////////////////////////////////////////////////////////////
#endif
